<?php
namespace Home\Org\Util;
class UserMappingItemcategory
{
    static private $_instance = null;
    private $model;
    private $userMappingItemcategoryResult; // SQL结果
    private $userMappingItemcategoryArray; // 针对大众，处理后的数组
    private $userArray; // SQL结果，用户数据
    private $userDevidedMappingItemcategoryArray; // 针对具体用户，处理后的数组

    static public function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new UserMappingItemcategory();
        }
        return self::$_instance;
    }

    public function getModel()
    {
        return $this->model;
    }

    private function __construct()
    {
        $this->model = M('userMappingItemcategory');
        $this->userMappingItemcategoryResult = $this->model->field('emp_no, itemcategory')->group('emp_no, itemcategory')->select();
        //var_dump($userMappingItemcategoryResult);die();
        $itemCategoryArray = array();
        foreach ($this->userMappingItemcategoryResult as $key => $item)
        {
            $tmpArray = explode(',',$item['itemcategory']);
            //var_dump($tmpArray);die();
            foreach ($tmpArray as $inKey => $inItem)
            {
                if(!in_array($inItem,$itemCategoryArray))
                {
                    $itemCategoryArray[$inItem] = $inItem;
                }
            }
        }
        $this->userMappingItemcategoryArray = $itemCategoryArray;
        $this->userDevidedMappingItemcategoryArray = $itemCategoryArray;
    }

    /**
     * @return mixed
     */
    public function getUserMappingItemcategoryResult()
    {
        return $this->userMappingItemcategoryResult;
    }

    /**
     * @return array
     */
    public function getUserMappingItemcategoryArray()
    {
        return $this->userMappingItemcategoryArray;
    }

    /**
     * @return mixed
     */
    public function getUserArray()
    {
        return $this->userArray;
    }

    public function getUserMappingByEmpNo($emp_no){
        $emp_no = strtolower($emp_no);
        foreach ($this->userMappingItemcategoryResult as $key => $item) {
            if($item['emp_no'] == $emp_no)
            {
                $this->userArray = $item;
                return $this->userArray;
            }
        }
        return;
    }

    public function getSpecialSQLStringByEmpNo($emp_no){
        $emp_no = strtolower($emp_no);
        $check = $this->getUserMappingByEmpNo($emp_no);

        if($check) {
            $categories = explode(',', $this->userArray['itemcategory']);
            foreach ($categories as $key => $item) {
                unset($this->userDevidedMappingItemcategoryArray[$item]);
            }
            return $this->getSQLString('special');
        }else{
            return $this->getSQLString('all');
        }
    }

    public function getSQLString($type){
        $tmpArray = array();
        if($type == 'all')
        {
            $tmpArray = $this->userMappingItemcategoryArray;
        }
        if($type == 'special')
        {
            $tmpArray = $this->userDevidedMappingItemcategoryArray;
        }
        $sqlString = '';
        if(!empty($tmpArray)) {
            $tmpList = " AND isnull(U_IT001,'') NOT IN (";
            foreach ($tmpArray as $key => $item) {
                $tmpList .= "'{$item}', ";
            }
            $sqlString = substr($tmpList, 0, $tmpList . count() - 2);
            $sqlString .= ")";
        }
        return $sqlString;
    }

}